{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6463a395-00ac-44f0-a23a-8c19ce955bfc",
   "metadata": {},
   "source": [
    "## 获取赢在起点的作业链接\n",
    "通过多线程的引入，大大加快了运行效率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5f415480-f4ae-49e3-97ff-cdea2802fc8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 找到每个序列的作业的地址\n",
    "import requests\n",
    "import string\n",
    "import time\n",
    "from concurrent.futures import ThreadPoolExecutor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "78d5cab9-056e-4292-8247-13b3734a1773",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finish fetch in 5.351064682006836 s and find 1 results\n"
     ]
    }
   ],
   "source": [
    "num1 = 3\n",
    "num2 = 7\n",
    "letters = string.ascii_lowercase\n",
    "\n",
    "# 使用嵌套循环生成所有排列\n",
    "hws = []\n",
    "session = requests.Session()\n",
    "def fetch(url):\n",
    "    try:\n",
    "        return session.get(url, timeout=5)\n",
    "    except requests.RequestException:\n",
    "        return None\n",
    "\n",
    "start = time.time()\n",
    "with ThreadPoolExecutor(max_workers=8) as executor:\n",
    "    urls= []\n",
    "    for first in letters:\n",
    "        for second in letters:\n",
    "            combination = first + second\n",
    "            url = f\"http://www.elitebaby.cn/qa/extend/MP{num1}.{num2}.{combination}.html\"\n",
    "            urls.append(url)\n",
    "            \n",
    "    results = list(executor.map(fetch, urls))\n",
    "    for response in results:\n",
    "        if response is not None and response.status_code == 200:\n",
    "                hws.append(url)\n",
    "        \n",
    "print(f\"finish fetch in {time.time()- start} s and find {len(hws)} results\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0c8450df-fa07-4144-9a1d-6a6728b3f4bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['http://www.elitebaby.cn/qa/extend/MP3.7.zz.html']\n"
     ]
    }
   ],
   "source": [
    "print(hws)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6482aaa7-cb73-40c1-8205-acb179fabdf2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d2ee4ef-3dab-464a-9c58-4a1cf7b1717c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
